<template>
    <section class="chart-container">
        <el-row>
            <el-col :span="12">
                <div id="chartColumn" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartBar" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartLine" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartPie" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="24">
                <a href="http://echarts.baidu.com/examples.html" target="_blank" style="float: right;">more>></a>
            </el-col>
        </el-row>
    </section>
</template>

<script>
    import echarts from 'echarts'

    export default {
        data() {

            return {
              chartColumn: null,
              chartBar: null,
              chartLine: null,
              chartPie: null,
              StatisticsClueInfoPie:[],
              StatisticsClueInfoLine:[],
            }
        },

        methods: {
          drawColumnChart() {
              this.chartColumn = echarts.init(document.getElementById('chartColumn'));
              this.chartColumn.setOption({
                title: { text: 'Column Chart' },
                tooltip: {},
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                  }]
              });
          },
          drawBarChart() {
              this.chartBar = echarts.init(document.getElementById('chartBar'));
              this.chartBar.setOption({
                  title: {
                      text: 'Bar Chart',
                      subtext: '数据来自网络'
                  },
                  tooltip: {
                      trigger: 'axis',
                      axisPointer: {
                          type: 'shadow'
                      }
                  },
                  legend: {
                      data: ['2011年', '2012年']
                  },
                  grid: {
                      left: '3%',
                      right: '4%',
                      bottom: '3%',
                      containLabel: true
                  },
                  xAxis: {
                      type: 'value',
                      boundaryGap: [0, 0.01]
                  },
                  yAxis: {
                      type: 'category',
                      data: ['巴西', '印尼', '美国', '印度', '中国', '世界人口(万)']
                  },
                  series: [
                      {
                          name: '2011年',
                          type: 'bar',
                          data: [18203, 23489, 29034, 104970, 131744, 630230]
                      },
                      {
                          name: '2012年',
                          type: 'bar',
                          data: [19325, 23438, 31000, 121594, 134141, 681807]
                      }
                  ]
              });
          },
          /*drawLineChart() {
             this.chartLine = echarts.init(document.getElementById('chartLine'));
            console.log("折线图",this.StatisticsClueInfoLine)

            this.StatisticsClueInfoLine = JSON.parse(this.StatisticsClueInfoLine);

            let time = this.getStatisticsClueInfoLine.map(x => x.date);
            let salesSum = this.getStatisticsClueInfoLine.map(x => x.salesSum);
            let salesCount = this.getStatisticsClueInfoLine.map(x => x.salesCount);
              this.chartLine.setOption({
                  title: {
                      text: 'Line Chart'
                  },
                  tooltip: {
                      trigger: 'axis'
                  },
                  legend: {
                      data: ['邮件营销', '联盟广告', '搜索引擎']
                  },
                  grid: {
                      left: '3%',
                      right: '4%',
                      bottom: '3%',
                      containLabel: true
                  },
                  xAxis: {
                      type: 'category',
                      boundaryGap: false,
                      data: time
                  },
                  yAxis: {
                      type: 'value'
                  },
                  series: [
                      {
                          name: '邮件营销',
                          type: 'line',
                          stack: '总量',
                          data: salesCount
                      },
                      {
                        name: '联盟广告',
                        type: 'line',
                        stack: '总量',
                        data: salesSum
                      }/!*,
                      {
                          name: '联盟广告',
                          type: 'line',
                          stack: '总量',
                          data: [220, 182, 191, 234, 290, 330, 310]
                      },
                      {
                          name: '搜索引擎',
                          type: 'line',
                          stack: '总量',
                          data: [820, 932, 901, 934, 1290, 1330, 1320]
                      }*!/
                  ]
              });
          },*/
          drawLineChart() {
            console.log("折线图",this.StatisticsClueInfoLine)

            this.StatisticsClueInfoLine = JSON.parse(this.StatisticsClueInfoLine);

            let time = this.StatisticsClueInfoLine.map(x => x.date);
            let salesSum = this.StatisticsClueInfoLine.map(x => x.salesSum);
            let salesCount = this.StatisticsClueInfoLine.map(x => x.salesCount);
            console.log("time",time)

            this.chartLine = echarts.init(document.getElementById('chartLine'));
            this.chartLine.setOption({
              title: {
                text: 'Line Chart'
              },
              tooltip: {
                trigger: 'axis'
              },
              legend: {
                data: ['订单金额', '订单笔数']
              },
              grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
              },
              xAxis: {
                type: 'category',
                boundaryGap: false,
                data: time
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  name: '订单金额',
                  type: 'line',
                  stack: '总量',
                  data: salesSum
                },
                {
                  name: '订单笔数',
                  type: 'line',
                  stack: '总量',
                  data: salesCount
                }
              ]
            });
          },

          drawPieChart() {
              this.chartPie = echarts.init(document.getElementById('chartPie'));
            //console.log("drawPieChart:",this.StatisticsClueInfoPie)
            //console.log(Object.prototype.toString.call(JSON.parse(this.StatisticsClueInfoPie)));
            this.StatisticsClueInfoPie = JSON.parse(this.StatisticsClueInfoPie);
            let name = this.StatisticsClueInfoPie.map(x => x.name);
            //console.log("name",name)
            this.chartPie.setOption({
                  title: {
                      text: 'Pie Chart',
                      subtext: '线索来源',
                      x: 'center'
                  },
                  tooltip: {
                      trigger: 'item',
                      formatter: "{a} <br/>{b} : {c} ({d}%)"
                  },
                  legend: {
                      orient: 'vertical',
                      left: 'left',
                      data: name
                  },
                  series: [
                      {
                          name: '访问来源',
                          type: 'pie',
                          radius: '55%',
                          center: ['50%', '60%'],
                          data: this.StatisticsClueInfoPie,
                         // data: [
                         //      { value: 335, name: '直接访问' },
                         //      { value: 310, name: '邮件营销' },
                         //      { value: 234, name: '联盟广告' },
                         //      { value: 135, name: '视频广告' },
                         //      { value: 1548, name: '搜索引擎' }
                         //  ],
                          itemStyle: {
                              emphasis: {
                                  shadowBlur: 10,
                                  shadowOffsetX: 0,
                                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                              }
                          }
                      }
                  ]
              });
          },
          getStatisticsClueInfoPie(){
              this.$http.post("/clue/getStatisticsClueInfoPie")
                  .then(result => {
                    result = result.data.obj;
                    this.StatisticsClueInfoPie = JSON.stringify(result);
                    this.drawPieChart()

              })
                  .catch(result => {

              })
          },
          getStatisticsClueInfoLine(){
            this.$http.post("/clue/getStatisticsClueInfoLine")
                .then(result => {
                  result = result.data;
                  this.StatisticsClueInfoLine = JSON.stringify(result.obj);
                  this.drawLineChart();
                })
                .catch(result => {

                })
          },
          drawCharts() {
              this.getStatisticsClueInfoPie()
              this.drawColumnChart()
              this.drawBarChart()
              this.getStatisticsClueInfoLine()

          },
        },

        mounted: function () {
            this.drawCharts()
        },
        updated: function () {
            this.drawCharts()
        }
    }
</script>

<style scoped>
    .chart-container {
        width: 100%;
        float: left;
    }
    /*.chart div {
        height: 400px;
        float: left;
    }*/

    .el-col {
        padding: 30px 20px;
    }
</style>
